import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_model/home/module_detail_model.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/widget/keep_alive_widget.dart';

import '../../../hj_model/home/home_tabs_entity.dart';
import '../../../tools_base/loading/l_error_widget.dart';
import '../../../tools_base/loading/loading_center_widget.dart';
import 'pic_grid_page.dart';
import 'pics_sub_logic.dart';

//图集子view
class PicsSubPage extends StatefulWidget {
  final ModuleData? tagData;
  const PicsSubPage({super.key, this.tagData});

  @override
  State<PicsSubPage> createState() => _PicsSubPageState();
}

class _PicsSubPageState extends State<PicsSubPage> {
  late PicsSubLogic logic;
  late String tag;

  @override
  void initState() {
    super.initState();
    tag = widget.tagData?.id ?? '';
    logic = Get.put(PicsSubLogic(tagData: widget.tagData), tag: tag);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PicsSubLogic>(
      tag: tag,
      builder: (_) {
        if (_.sections == null) return LoadingCenterWidget();
        if (_.sections != null && _.sections?.isEmpty == true) return CErrorWidget();
        return Row(
          children: [
            _buildLeftMenu(),
            Expanded(child: _buildRightMenus()),
          ],
        );
      },
    );
  }

  //左侧菜单
  _buildLeftMenu() {
    return Container(
      width: 75,
      child: GetBuilder<PicsSubLogic>(
        tag: tag,
        builder: (_) => ListView.builder(
          padding: EdgeInsets.zero,
          itemExtent: 36,
          itemCount: _.sections?.length ?? 0,
          itemBuilder: (context, index) {
            bool select = _.selectIndex == index;
            AllSection? model = _.sections?[index];
            return InkWell(
              onTap: () => _.onToggleItem(index),
              child: Container(
                  color: select ? Color(0xff207FEF) : Color(0xff181926),
                  child: Row(
                    children: [
                      10.sizeBoxW,
                      Text(
                        model?.sectionName ?? '',
                        style: textStyle(
                          12,
                          select ? Colors.black : Color(0xffA3A3A3),
                          FontWeight.w400,
                        ),
                      ),
                    ],
                  )),
            );
          },
        ),
      ),
    );
  }

  //右侧菜单
  _buildRightMenus() {
    return Padding(
      padding: EdgeInsets.fromLTRB(6, 0, 6, 0),
      child: PageView.builder(
        physics: NeverScrollableScrollPhysics(),
        controller: logic.pageController,
        itemCount: logic.sections?.length,
        itemBuilder: (context, index) => PicGridPage(
          section: logic.sections?[index],
        ).keepAlive,
      ),
    );
  }
}
